package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    private PanelContentMapper contentMapper;
    @Autowired
    private PanelMapper panelMapper;
    @Autowired
    private ContentConverter contentConverter;

    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        Long cid = pageInfo.getCid();
        String infoSort = pageInfo.getSort();
        Integer priceMin = pageInfo.getPriceGt();
        Integer priceMax = pageInfo.getPriceLte();
        String sort = (infoSort == null || "".equals(infoSort)) ? "created" : "price";
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();

        // 添加筛选条件
        if (cid != null) {
            criteria.andEqualTo("cid", cid);
        }
        if (priceMin != null) {
            criteria.andGreaterThanOrEqualTo("price", priceMin);
        }
        if (priceMax != null) {
            criteria.andLessThanOrEqualTo("price", priceMax);
        }

        // 添加排序条件
        if ("1".equals(infoSort)) {
            example.orderBy(sort).asc();
        } else {
            example.orderBy(sort).desc();
        }
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        List<Item> items = itemMapper.selectByExample(example);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        List<Item> list = itemPageInfo.getList();
        long total = itemPageInfo.getTotal();
        AllProductResponse productResponse = new AllProductResponse();
        List<ProductDto> productDtoList = productConverter.items2Dto(list);
        productResponse.setData(productDtoList);
        productResponse.setTotal(total);
        return productResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("position",1);
        List<Panel> panels = panelMapper.selectByExample(example);
        Set<PanelDto> panelDtos = new HashSet<>();
        for (Panel panel : panels) {
            Integer limitNum = panel.getLimitNum();
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            List<PanelContentItem> contentItems = contentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(
                    contentItems.subList(0, Math.min(contentItems.size(), limitNum))
            );
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
        }
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
